package com.jbl.te.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbl.commonutils.JwtUtils;
import com.jbl.commonutils.MD5;
import com.jbl.exceptionhandler.ManagerException;
import com.jbl.te.entity.User;
import com.jbl.te.mapper.UserMapper;
import com.jbl.te.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public String login(User user) {

        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            throw new ManagerException(20001,"登录失败");
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User one = this.getOne(wrapper);
        if(one == null) {
            throw new ManagerException(20001,"用户名不存在！");
        }

        if(!MD5.encrypt(user.getPassword()).equals(one.getPassword()) ) {
            throw new ManagerException(20001,"用户名或密码错误！");
        }

        String token = JwtUtils.getJwtToken(one.getId());
        return token;
    }
}
